package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysUserMapper;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.ProjectQuery;
import cn.ljy.uem.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * @description 项目模块的业务层接口
 * @author  王秋林
 * @date 2023-1-16 10:45
 */
@Service
@Slf4j
public class IProjectService extends IBaseService<Project, ProjectQuery> implements ProjectService {

    @Autowired
    private ProjectMapper mapper;

    @Autowired
    private SysUserMapper userMapper;


    @Override
    protected void beforeGet(Serializable id) {
        if (mapper.findById(id) == null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void afterGet(Project project) {
        if (project.getManagement()!=null){
            project.setManagementUser(userMapper.findById(project.getManagement()));
        }
    }

    @Override
    protected void beforeUpdate(Project project) {
        if (project.getEndTime()!=null && project.getBeginTime()!=null){
            if(project.getEndTime().before(project.getBeginTime())){
                throw new MyWebException("修改失败：开始时间在在时间之后!");
            }
        }
    }

    @Override
    protected void beforeSave(Project project) {
        if (userMapper.findById(project.getManagement())==null){
            throw new MyWebException("添加失败：请选择项目经理");
        }
        if (project.getEndTime()!=null && project.getBeginTime()!=null){
            if(project.getEndTime().before(project.getBeginTime())){
                throw new MyWebException("添加失败：开始时间在在时间之后!");
            }
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        //后续需添加
    }

    @Override
    protected void beforeFindAll(ProjectQuery query) {

    }

    //行政班多表查询
    @Override
    protected void afterFindAll(List<Project> list) {
        for (Project project : list){
            if (project.getManagement()!=null){
                project.setManagementUser(userMapper.findById(project.getManagement()));
            }
        }
    }

}
